<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

if (isset($_POST['type']) && $_POST['type'] == "post")
{
	$date = UV::readMQ($route->POST('date'));
	$time = UV::readMQ($route->POST('time'));
	$sname = UV::readMQ($route->POST('sname'));
	$name = UV::readMQ($route->POST('name'));
	$tname = UV::readMQ($route->POST('tname'));
	$login = UV::readMQ($route->POST('login'));
	$domain = UV::readMQ($route->POST('domain'));
	$sex = UV::readMQ($route->POST('sex'));
	$age = UV::readInt($route->POST('age'));
	$city = UV::readInt($route->POST('city'));
	$area = UV::readInt($route->POST('area'));
	$street = UV::readMQ($route->POST('street'));
	$comment = UV::readMQ($route->POST('comment'));
	$date = strtotime($date." ".$time);
	
	$reports = $db->sendQuery("insert into `reports` values (NULL,'".$user->getUserId()."','".time()."')");
	$report = $db->sendQuery("insert into `joined` values (NULL,'".mysql_insert_id()."','$date','$sname','$name','$tname','".$login."@".$domain.".ru','$sex','$age','$city','$area','$street','$comment')");
	
	if (UV::readBool($route->POST('friend')))
	{
		$user_id = 0;
		switch($user->getUserId())
		{
			case  7:	$user_id = 8; break;
			case  5:	$user_id = 6; break;
			case  9:	$user_id = 11; break;
			case 17:	$user_id = 18; break;
		}
		if ($user_id != 0)
		{
			$report_2 = $db->sendQuery("insert into `reports` values (NULL,'$user_id','".time()."')");
			$friend = $db->sendQuery("insert into `friends` values (NULL,'".mysql_insert_id()."','$login','$domain','$sname','$name','$tname','$date','$sex','$age','$city','$area','$street','','')");
		}
	}
	
	require_once COORDINATOR.DS."head.php";
	if (!$report || !$reports)
		echo "Ошибка! Проверьте введенные данные<br />";
	echo "<a href='".$_SERVER['REQUEST_URI']."'>Добавить еще</a>";
}
else
{
	require_once COORDINATOR.DS."head.php";
	?>
	<script type='text/javascript'>
	$(document).ready(function(){
		$("#reportForm").submit(function(){
			var date = /^[0-9]{2}\.[0-9]{2}\.201[1-2]{1}$/;
			var time = /^[0-9]{2}:[0-9]{2}$/;
			
			if (!date.test($("#date").val()))
			{
				makeRed($("#date"));
				return false;
			}
			if (!time.test($("#time").val()))
			{
				makeRed($("#time"));
				return false;
			}
			if ($("#login").val() == "")
			{
				makeRed($("#login"));
				return false;
			}
			return confirm("Отправить отчет?");
		});
		
		function makeRed(obj){
			obj.css("border","1px solid red").focus();
		}
		
		$(".enter").keydown(function(){
			$(this).css("border","");
		})
		
		$("#city").change(function(){
			$.ajax({
					url: "/coordinator/getarea?city="+$(this).val(),
					success: function(data){
						$("#area option").remove();
						if (data != "")
						{
							cities = $.parseJSON(data);
							for (var i=0; i<cities.length; i++)
								$("#area").append("<option value='"+cities[i].area_id+"'>"+cities[i].name+"</option>");
						}
					}
			})
		});
	})
	</script>
	<form method="POST" id='reportForm'>
		<input type='hidden' name='type' value='post' />
		<table cellpadding='0' cellspacing='5' style='width:600px;' border='0'>
			<tr>
				<td width="200px"><label for="date">Дата</label></td>
				<td width="400px"><input type='text' name='date' id='date' class="enter date" style="width:100%" /></td>
				<td width="100px"><label for="time">Время</label></td>
				<td width="500px"><input type='text' name='time' id='time' class="enter time" style="width:100%" /></td>
			</tr>
			<tr>
				<td colspan='2' class="hint"><span>Формат: 01.01.2011</span></td>
				<td colspan='2' class="hint"><span>Формат: 12:00</span></td>
			</tr>
			<tr>
				<td><label for="sname">Фамилия</label></td>
				<td colspan='3'><input type='text' name='sname' id='sname' class="enter" style="width:100%" /></td>
			</tr>
			<tr>
				<td><label for="name">Имя</label></td>
				<td colspan='3'><input type='text' name='name' id='name' class="enter" style="width:100%" /></td>
			</tr>
			<tr>
				<td><label for="tname">Отчество</label></td>
				<td colspan='3'><input type='text' name='tname' id='tname' class="enter" style="width:100%" /></td>
			</tr>
			<tr>
				<td><label for="login">E-mail</label></td>
				<td colspan='2'><input type='text' name='login' id='login' class="enter" style="width:100%" /></td>
				<td>
					<select name='domain' id='domain' style="width:100%">
						<option value='mail'>@mail.ru</option>
						<option value='inbox'>@inbox.ru</option>
						<option value='bk'>@bk.ru</option>
						<option value='list'>@list.ru</option>
					</select>
				</td>
			</tr>
			<tr>
				<td><label for="sex">Пол</label></td>
				<td>
					<select name='sex' id='sex'>
						<option value='undefined'>не известно</option>
						<option value='male'>мужской</option>
						<option value='female'>женский</option>
					</select>
				</td>
				<td><label for="age">Возраст</label></td>
				<td><input type='text' name='age' id='age' class="enter" style="width:50%" /> лет</td>
			</tr>
			<tr>
				<td><label for="city">Город</label></td>
				<td>
					<select name='city' id='city' style="width:100%"><?php
						$oren = $db->getCell("select `city_id` from `cities` where `name` like 'Оренбург' limit 1");
						$not_in_ob = $db->getCell("select `city_id` from `cities` where `name` like 'Не в Оренбургской области' limit 1");
						$cities = $db->getTable("select * from `cities` where `city_id` != '$oren' and `city_id` != '$not_in_ob' order by `name`");
						echo "<option value='$not_in_ob'>Не в Оренбургской области</option>";
						echo "<option value='$oren'>Оренбург</option>";
						foreach($cities as $city)
							echo "<option value='$city[city_id]'>$city[name]</option>";
					?></select>
				</td>
				<td><label for="area">Район</label></td>
				<td>
					<select name='area' id='area' style="width:100%"><?php
						/* $areas = $db->getTable("select * from `areas` where `city_id` = '$oren' order by `name`");
						foreach($areas as $area)
							echo "<option value='$area[area_id]'>$area[name]</option>"; */
					?></select>
				</td>
			</tr>
			<tr>
				<td><label for="street">Улица</label></td>
				<td colspan='3'><input type='text' name='street' class='enter' id='street' /></td>
			</tr>
			<tr>
				<td><label for="friend">В друзьях</label></td>
				<td colspan='3'><input type='checkbox' name='friend' id='friend' /></td>
			</tr>
			<tr>
				<td valign='top'><label for="comment">Ваш комментарий</label></td>
				<td colspan='3'><textarea name='comment' id='comment' style='width:100%' rows='5'></textarea></td>
			</tr>
			<tr>
				<td colspan='4' align="right"><input type='submit' value="Формировать отчет" style='width:200px;height:35px' /></td>
			</tr>
		<table>
	</form><?php
}

require_once COORDINATOR.DS."footer.php";
?>